define(function(require, exports, module) {
	
	var procdefTpl=require('resources/template/workflow/procdef_tpl.html');
	require('resources/js/jquery.dataTables.min.js');
	require('resources/js/jquery.dataTables.bootstrap.js');
	require('resources/js/jquery.dataTables.buttons.js');
	require('resources/js/jquery.json-2.4.min.js');
	require('resources/js/jquery.gritter.min.js');
	require('resources/js/ajaxfileupload.js');
	var DTConstant=require('resources/template/common/DTConstant.js');
	var ACEConstant=require('resources/template/common/ACEConstant.js');
	
	var Procdef=function($container,config,data){
		this.getUrl=Common.getUrl.bind(this);
		this.config=$.extend({},Procdef.DEFAULT,config);
		this.data=data;
		this.$container=$container;
		this.$div=$(procdefTpl).prependTo(this.$container);
		this.init();
	};
	
	Procdef.prototype={
		urls:{
			list:'/procdef/list.jhtml',
			deleteProcdef:'/procdef/delete.jhtml',
			updateStateProcdef:'/procdef/updateState.jhtml',
			convert2MProcdef:'/procdef/convert2M.jhtml',
			deployProcdef:'/procdef/deploy.jhtml',
			viewProcdef:'/procdef/view.jhtml?procdefId=',
			exportImageModel:'/procdef/export.jhtml?type=image&procdefId=',
			exportXmlModel:'/procdef/export.jhtml?type=xml&procdefId='
		},	
		init:function(){
			//初始化-列表
			this.initList();
			//流程图查看弹出框
			this.$procdefShow=$('.procdefShowModal',this.$div);
			this.$procdefShowTitle=$('h4',this.$div);
			this.$procdefShowImg=$('img',this.$div);
			//部署流程弹出框
			this.initProcdefDeploy();
		},
		initProcdefDeploy:function(){
			var that=this;
			var tpl='<input multiple="" type="file" class="input-procdef-file" name="importFile" />'+
                '<label><input type="checkbox" name="file-format" id="id-file-format" class="ace" /></label>';
			this.$procdefDeploy=$('.procdefDeployModal',this.$div);
			this.$procdefDeployBody=$('.modal-body',this.$procdefDeploy);
			this.$procdefDeployBody.append(tpl);
			var initProcdefFileInput=function(){
				that.$procdefDeployInput=$('.input-procdef-file',that.$procdefDeploy);
				that.$procdefDeployInput.ace_file_input({
					style:'well',
					btn_choose:'请选择或拖放zip,bar,bpmn,bpmn20文件',
					btn_change:null,
					no_icon:'icon-cloud-upload',
					droppable:true,
					thumbnail:'small',
					whitelist:'xml|zip|bar',
					before_change:function(files, dropped) {
						var allowed_files = [];
						for(var i = 0 ; i < files.length; i++) {
							var file = files[i];
							if(typeof file === "string") {
								//IE8 and browsers that don't support File Object
								if(! (/\.(bar|zip|xml|text)$/i).test(file) ) return false;
							}
							else {
								var type = $.trim(file.type);
								if( ( type.length > 0 &&  !(/\/(bar|zip|xml|text)$/i).test(type) )
										|| ( type.length == 0 &&  !(/\.(bar|zip|xml|text)$/i).test(file.name) )//for android's default browser which gives an empty string for file.type
									) continue;
							}
							
							allowed_files.push(file);
						}
						if(allowed_files.length == 0) return false;
						return allowed_files;
					}
				});
			};
			initProcdefFileInput();
			$('.btn-save',this.$procdefDeploy).on('click',function(){
					var now=new Date(),fileId=now.getHours()+'-'+now.getMinutes()+'-'+now.getSeconds();
					that.$procdefDeployInput.attr('id',fileId);
					$.ajaxFileUpload({
	                    url: that.getUrl('deployProcdef'), //用于文件上传的服务器端请求地址
	                    fileElementId: fileId, //文件上传域的ID
	                    dataType: 'json', //返回值类型 一般设置为json
	                    success: function (data, status){  //服务器成功响应处理函数
	                    	if(data.result)
							{
	                    		//刷新list
								that.refreshList();
								//提示成功
								ACEConstant.operateSuccess('','部署流程成功');
								//关掉modal
								that.$procdefDeploy.modal('hide');
							}	
							else
								ACEConstant.operateFailed(title,'部署流程失败：'+data.note);
	                    	
	                    	that.$procdefDeployBody.empty();
							that.$procdefDeployBody.append(tpl);
							initProcdefFileInput();
	                    },
	                    error: function (data, status, e){//服务器响应失败处理函数
	                    	ACEConstant.operateFailed('','部署流程失败');	
	                    	that.$procdefDeployBody.empty();
							that.$procdefDeployBody.append(tpl);
							initProcdefFileInput();
	                    }
	                });
			});
		},
		showProcdef:function(procdef){
			var title='流程图:'+procdef.name;
			this.$procdefShowTitle.text(title);
			this.$procdefShowImg.attr('alt',title);
			this.$procdefShowImg.attr('src',this.getUrl('viewProcdef')+procdef.id);
			this.$procdefShow.modal('show');
		},
		openProcdefDeploy:function(){
			this.$procdefDeploy.modal('show');
		},
		refreshList:function(){
			this.table.ajax.reload();
		},
		initList:function(){
			var that=this;
			this.$list=$('.procdefList',this.$div);
			this.table =this.$list.DataTable($.extend(true,{},DTConstant.DEFAULT,{
				ajax:{
				    url:this.getUrl('list'),
				    contentType: "application/json",
				    type: "POST",
				    data: function ( d ) {
				      return $.toJSON(d);
				    }
				},
				rowId:'id',
				columns:[{data:'id',title:''},
				         {data: 'name' ,title:'名称'},
				         {data: 'key' ,title:'标识'},
				         {data: 'category',title:'分类' ,render:DTConstant.RENDER.ELLIPSIS(20)},
				         {data: 'version',title:'版本'},
				         {data: 'description',title:'描述'},
				         {data: 'suspensionState' ,title:'状态'},
				         {data: 'deployDT' ,title:DTConstant.TITLE.DATETIME+'部署时间'}
				],
				columnDefs: [DTConstant.COLUMN.RADIO],
				buttons:{
					buttons: [{
						text: '激活/挂起',
						className: 'btn btn-sm btn-success icon-lightbulb btn-state',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							
							var title=(record.suspensionStateCode?'激活':'挂起')+'流程：'+record.name;
							$.ajax(that.getUrl('updateStateProcdef'),{
								type:'POST',
								dataType:'json',
								data:{procdefId:record.id,state:record.suspensionStateCode}
							}).done(function(data){//成功
								if(data.result)
								{
									that.refreshList();
									ACEConstant.operateSuccess(title);
									$('.btn-state span',that.$div).text('激活/挂起');
								}	
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '查看',
						className: 'btn btn-sm btn-success icon-eye-open',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							that.showProcdef(record);
						}
					},{
						text: '删除',
						className: 'btn btn-sm btn-success icon-trash',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							var title='删除流程定义：'+record.name;
							$.ajax(that.getUrl('deleteProcdef'),{
								type:'POST',
								dataType:'json',
								data:{deploymentId:record.deploymentId}
							}).done(function(data){//成功
								if(data.result)
								{
									that.refreshList();
									ACEConstant.operateSuccess(title);
								}	
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '导出',
						className: 'btn btn-sm btn-success icon-external-link',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							window.open(that.getUrl('exportXmlModel')+record.id);
							window.open(that.getUrl('exportImageModel')+record.id);
						}
					},{
						text: '部署',
						className: 'btn btn-sm btn-success icon-download-alt',
						action: function ( e, dt, node, config ) {
							that.openProcdefDeploy();
						}
					},{
						text: '生成模型',
						className: 'btn btn-sm btn-success icon-cog',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							var title='生成模型：'+record.name;
							$.ajax(that.getUrl('convert2MProcdef'),{
								type:'POST',
								dataType:'json',
								data:{procdefId:record.id}
							}).done(function(data){//成功
								if(data.result)
									ACEConstant.operateSuccess(title);
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '刷新',
						className: 'btn btn-sm btn-success icon-refresh',
						action: function ( e, dt, node, config ) {
							that.refreshList();
						}
					}]
				}
			}));
			DTConstant.enableRadio(this.$list,this.table,function(selected,record){
				var title='激活/挂起';
				if(selected)
					title=record.suspensionStateCode?'激活':'挂起';
				$('.btn-state span',that.$div).text(title);
			});
		},
		destroy:function(){
			this.$div.remove();
			this.$vmId&&delete avalon.vmodels[this.$vmId];
		}
	}
	
	Procdef.DEFAULT={
			
	};
	
	return Procdef;
});